package javaweb.oa.web.action;

import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import javaweb.oa.web.action.Utils.DBUtil;
import jdk.jshell.execution.Util;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class DeptDetailServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        String deptno = request.getParameter("deptno");

out.print("        <!DOCTYPE html>");
out.print("<html>");
out.print("	<head>");
out.print("		<meta charset='UTF-8'>");
out.print("		<title>部门详情</title>");
out.print("	</head>");
out.print("	<body>");
out.print("		<h1>部门详情</h1>");
out.print("		<hr>");
out.print("                部门编号:"+deptno+" <br>");

        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try{
            conn = DBUtil.getConnection();
            String sql = "select deptno, dname, loc from dept where deptno="+deptno+"";
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            while(rs.next()){
                String dname = rs.getString("dname");
                String loc = rs.getString("loc");

                out.print("部门名称:"+dname+" <br>");
                out.print("部门地址:"+loc+" <br>");
            }

        }catch(Exception e){
            e.printStackTrace();
        }finally{
            DBUtil.close(conn,ps,rs);
        }
out.print("        <input type='button' value='后退' onclick='window.history.back()'/>");
out.print("	</body>");
out.print("</html>");
    }
}
